<!-- 会员- 查看优惠券 - 弹窗组件 -->
<template>
  <div>
    <CustomDialog v-model:visible="isShowDialog" top="2vh" curent-class="check-coupon-dialog">
      <div class="dialog-box">
        <div class="left-box">
          <div class="title">优惠券</div>
          <div class="scroll-list" v-loading="loading" element-loading-text="加载中...">
            <template v-if="couponList.length && !loading">
              <div class="scroll-item" v-for="(item, index) in couponList" :key="index">
                <template v-if="item.is_expire === 0 || item.is_expire === 2">
                  <!-- 满减券 -->
                  <img
                    v-if="item.coupon_type.value === 10"
                    class="bg-img"
                    :src="manjianquanBg"
                    alt=""
                  />
                  <!-- 现金券 -->
                  <img
                    v-if="item.coupon_type.value === 30"
                    class="bg-img"
                    :src="zhekouquanBg"
                    alt=""
                  />
                </template>
                <!-- 已过期 -->
                <img class="bg-img" v-if="item.is_expire === 1" :src="yiguoqiBg" alt="" />
                <div class="scroll-item-left">
                  <div class="reduce-price">
                    <span v-if="item.coupon_type.value === 30" style="color: #fff">￥</span>
                    <span v-else>￥</span>
                    <div v-if="item.coupon_type.value === 30" style="color: #fff">
                      {{ item.reduce_price }}
                    </div>
                    <div v-else>{{ item.reduce_price }}</div>
                  </div>
                </div>
                <div class="scroll-item-right">
                  <div class="reduce-name">
                    <span v-if="item.coupon_type.value === 30" style="color: #fff">{{
                      item.coupon_type.text
                    }}</span>
                    <span v-else>{{ item.coupon_type.text }}</span>
                  </div>
                  <div class="reduce-desc">
                    <span v-if="item.coupon_type.value === 30" style="color: #fff"
                      >满{{ item.min_price }}可用</span
                    >
                    <span v-else>满{{ item.min_price }}可用</span>
                  </div>
                  <div class="reduce-time" v-if="item.is_expire === 0">
                    <span v-if="item.coupon_type.value === 30" style="color: #fff"
                      >有效期至{{ item.end_time }}</span
                    >
                    <span v-else>有效期至{{ item.end_time }}</span>
                  </div>
                  <div
                    class="over-time"
                    :style="{ color: item.coupon_type.value === 30 ? '#fff' : '#7c0a03' }"
                    v-if="item.is_expire === 1"
                  >
                    已过期
                  </div>
                  <div
                    class="over-time"
                    :style="{ color: item.coupon_type.value === 30 ? '#fff' : '#7c0a03' }"
                    v-if="item.is_expire === 2"
                  >
                    未到可用时间
                  </div>
                </div>
              </div>
              <!-- 显示多少张优惠券 -->
              <div class="position-coupon-count" v-if="couponList.length">
                共{{ couponList.length }}张
              </div>
            </template>
            <!-- 暂无数据的组件 -->
            <div class="no-data-box" v-else-if="!loading">
              <NotData desc="暂无优惠券" />
            </div>
          </div>
        </div>
      </div>
    </CustomDialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import NotData from '@/components/NotData/Index.vue'
import * as memberApi from '@/api/member'

interface Props {
  visible: boolean
  memberId: string
}

interface Emits {
  (e: 'update:visible', value: boolean): void
}

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  memberId: ''
})

const emit = defineEmits<Emits>()

// 控制弹窗显示
const isShowDialog = computed({
  get: () => props.visible,
  set: val => emit('update:visible', val)
})

// 优惠券列表
const couponList = ref<any[]>([])

// loading状态
const loading = ref(false)

// 图片资源
const manjianquanBg = new URL('@/assets/images/coupon/manjianquan-bg.png', import.meta.url).href
const zhekouquanBg = new URL('@/assets/images/coupon/zhekouquan-bg.png', import.meta.url).href
const yiguoqiBg = new URL('@/assets/images/coupon/yiguoqi-bg.png', import.meta.url).href

// 监听弹窗显示状态
watch(
  () => props.visible,
  newVal => {
    if (newVal) {
      getCouponList()
    }
  }
)

// 获取优惠券列表
const getCouponList = () => {
  loading.value = true
  memberApi
    .fetchCouponList({
      user_id: props.memberId
    })
    .then(res => {
      if (res.status === 200) {
        couponList.value = res?.data?.list?.length > 0 ? res.data.list : []
      }
    })
    .catch(error => {
      console.error('获取优惠券列表失败:', error)
    })
    .finally(() => {
      loading.value = false
    })
}
</script>

<style lang="scss" scoped>
/* 滚动条整体的大小 */
.scroll-list::-webkit-scrollbar {
  width: 6px !important;
  height: 0 !important;
}

/* 滚动条里滑动的滑块 */
.scroll-list::-webkit-scrollbar-thumb {
  height: 200px !important;
  background: #d2d2d2 !important;
  border-radius: 3px !important;
}

/* 滚动条底层样式 */
.scroll-list::-webkit-scrollbar-track {
  display: none !important;
}

.dialog-box {
  display: flex;
  flex-direction: row;

  .left-box {
    width: 477px;

    .title {
      font-size: 20px;
      font-family:
        PingFang SC,
        PingFang SC-Bold;
      font-weight: 700;
      text-align: left;
      color: #333333;
    }

    .scroll-list {
      position: relative;
      box-sizing: border-box;
      width: 477px;
      height: 635px;
      border: 1px solid #d2d2d2;
      border-radius: 12px;
      padding: 0 30px 100px 30px;
      margin-top: 30px;
      overflow-y: auto;
      overflow-x: hidden;

      .scroll-title {
        font-size: 16px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #333333;
        margin-top: 20px;
      }

      .scroll-item {
        position: relative;
        box-sizing: border-box;
        width: 417px;
        height: 147px;
        padding-left: 30px;
        display: flex;
        align-items: center;
        margin-top: 20px;
        cursor: pointer;

        .bg-img {
          position: absolute;
          top: 0;
          left: 0;
          z-index: 1;
          width: 417px;
          height: 147px;
        }

        .choose-img {
          position: absolute;
          top: 0;
          right: 0;
          z-index: 2;
          width: 75px;
          height: 75px;
        }

        .scroll-item-left {
          position: relative;
          z-index: 3;
          margin-right: 40px;

          .reduce-price {
            display: flex;
            align-items: flex-end;
            font-size: 20px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #7c0a03;

            > div {
              font-size: 50px;
            }
          }

          .discount-price {
            display: flex;
            align-items: flex-end;
            width: 50px;
            font-size: 20px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #ffe2c2;

            > div {
              font-size: 50px;
            }
          }
        }

        .scroll-item-right {
          position: relative;
          z-index: 3;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;

          .reduce-name {
            font-size: 30px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #7c0a03;
          }

          .reduce-desc {
            margin-top: 6px;
            font-size: 18px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #7c0a03;
          }

          .reduce-time {
            margin-top: 12px;
            font-size: 14px;
            font-family:
              PingFang SC,
              PingFang SC-Regular;
            font-weight: 400;
            color: #7c0a03;
          }

          .discount-name {
            font-size: 30px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #ffe2c2;
          }

          .over-time,
          .discount-time {
            margin-top: 24px;
            font-size: 14px;
            font-family:
              PingFang SC,
              PingFang SC-Regular;
            font-weight: 400;
            color: #c89359;
          }
        }
      }

      .position-coupon-count {
        position: fixed;
        bottom: 60px;
        left: 60px;
        height: 80px;
        background: #fff;
        width: 460px;
        z-index: 10;
        box-sizing: border-box;
        padding-left: 30px;
        display: flex;
        align-items: center;
        font-size: 14px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #333333;
        border-top: none;
      }
    }
  }
}

.no-data-box {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 170px;
  margin-top: 150px;
  width: 100%;
}
</style>

<style lang="scss">
.check-coupon-dialog {
  max-height: unset !important;
}
</style>
